﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ionic.Zip;
using System.IO;

namespace CRPViewer
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Count() != 1)
            {
                return;
            }

            string crpFileName = args[0];

            if (!crpFileName.EndsWith(".crp") || !File.Exists(crpFileName))
            {
                Console.WriteLine("Expected a CRP-file name as an argument.");
                return;
            }

            Console.WriteLine("Reading {0}...", crpFileName);

            string extractFolderName = Path.GetFileNameWithoutExtension(crpFileName);
            string tempFolderFullName = Path.GetTempPath() + extractFolderName;

            using (ZipFile zip = ZipFile.Read(crpFileName))
            {
                foreach (ZipEntry e in zip)
                {
                    e.Extract(tempFolderFullName, ExtractExistingFileAction.OverwriteSilently);  // overwrite == true
                }
            }

            if (!Directory.Exists(String.Format(@"{0}\Before\", tempFolderFullName))
             || !Directory.Exists(String.Format(@"{0}\After\", tempFolderFullName)))
            {
                Console.WriteLine("CRP-file is malformed.");
                return;
            }

            //Create process
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

            //strCommand is path and file name of command to run
            pProcess.StartInfo.FileName = @"WinDiff.exe";

            //strCommandParameters are parameters to pass to program
            pProcess.StartInfo.Arguments = String.Format(@"""{0}\Before"" ""{0}\After""", tempFolderFullName);

            pProcess.StartInfo.UseShellExecute = false;

            //Set output of program to be written to process output stream
            pProcess.StartInfo.RedirectStandardOutput = true;

            //Start the process
            pProcess.Start();

            //Get program output
            string strOutput = pProcess.StandardOutput.ReadToEnd();

            //Wait for process to finish
            pProcess.WaitForExit();

            // Clean up
            DirectoryInfo downloadedMessageInfo = new DirectoryInfo(tempFolderFullName);
            downloadedMessageInfo.Delete(true);
        }
    }
}
